# coding=utf-8
# coding=utf-8

import tornado.web

import WrapCursor
from WorkManager import *


class DelTimingSwitch(tornado.web.RequestHandler):
    __metaclass__ = ABCMeta

    def post(self):
        print(self.request.path)
        ojson = None
        r = WrapCursor.WrapCursor()
        id = None
        try:
            ojson = json.loads(self.request.body)
            desc = ojson['id']
            CheckJsonKeys(ojson, 'username', 'userpass')
            CheckAuth(ojson)
        except:
            self.write(r.Error('request data error'))
            return
        id = ojson['id']
        sql = None
        ret = ""
        data_set = None
        col_name = None
        del_row = 0
        if id == "*":
            self.write(r.Error('you are attack server,warning'))
            return
        else:
            sql = """
                delete from TimingSwitch where id == ?
            """
            try:
                cur.execute(sql, (id,))
                con.commit()
                del_row = con.total_changes
            except Exception as e:
                self.write(r.Error(str(e)))
                return
        if del_row == 0:
            self.write(r.Error("can not del normal id error"))
        else:
            self.write(r.Ok('del normal ok,delete rows:' + str(del_row)))
        return
